//
//  XLUserAccountTools.swift
//  WeiBo
//
//  Created by macbook on 16/10/18.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

class XLUserAccountTools: NSObject {

    //保存路径
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    //保存数据的对象
    var userAccout:XLUserAccout?
    
    //判断用户是否登陆
    var isLogin:Bool
    {
        //tonek为空代表没有登陆
       return accessToken != nil
    }
    
    
    
    var accessToken:String?{
        
        //代表没有登录
        if userAccout?.access_token == nil {
            
            return nil
        }else {
            
            //如果expires_Date 和当前时间比较是降序 代表没有过时
            if userAccout?.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
                return userAccout?.access_token
            }else{
                
                return nil
            }
        }
    }

    
    //设置全局访问点
    static let shardTools:XLUserAccountTools = XLUserAccountTools()
    
    override init() {
        super.init()
        userAccout = getUserAccount()
    }
    
    //保存对象
    func saveUserAccount(userAccoun:XLUserAccout)
    {
        //给对象赋值
        self.userAccout = userAccoun
        
        //保存
        NSKeyedArchiver.archiveRootObject(userAccoun, toFile: path)
        
    }
    
    //获取对象
    func getUserAccount()->XLUserAccout?
    {
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? XLUserAccout
        
        return result
    }
    
    
    
}







